package com.ljfiction.book.fragment.ranking;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ljfiction.book.R;
import com.ljfiction.book.adapter.HomRankingLiftAdapter;
import com.ljfiction.book.fragment.BaseFragment;
import com.ljfiction.book.fragment.ranking.RankNotatleFragment;
import com.ljfiction.book.fragment.ranking.RankThreetatleFragment;
import com.ljfiction.book.fragment.ranking.RankTowtatleFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/5.
 */
@ContentView(R.layout.fragment_ranking)
public class RankingFrament extends BaseFragment implements AdapterView.OnItemClickListener{
    private Context context;
    public static int mPosition;
    private HomRankingLiftAdapter adapter;
    private ArrayList<String> datas=new ArrayList<>();
    private RankTowtatleFragment fragment1,fragment8;
    private RankNotatleFragment fragment4;
    private RankThreetatleFragment fragment2,fragment3,fragment5,fragment6,fragment7;
    private int idd;
    @ViewInject(R.id.listviewLift_ranking)
    ListView listView;
    private FragmentManager fragmentManager;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = x.view().inject(this,inflater, container);
        context = getActivity();
        idd=R.id.fragment_container;
        fragmentManager=getChildFragmentManager();
        fragment1=RankTowtatleFragment.newInstance(context,1);
        addFragment(fragmentManager.beginTransaction(), idd, fragment1, false);
        intData();
        intView();
        return view;
    }

    private void intView() {
        listView.setDivider(null);
        adapter = new HomRankingLiftAdapter(datas);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    private void intData() {
        datas.add("金椒榜");
        datas.add("小米椒");
        datas.add("点击榜");
        datas.add("新书榜");
        datas.add("畅销榜");
        datas.add("吐槽榜");
        datas.add("更新榜");
        datas.add("打赏榜");
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        //拿到当前位置
        mPosition = position;
        //即使刷新adapter
        adapter.notifyDataSetChanged();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (position){
            case 0:
                if (fragment1==null){
                    fragment1=RankTowtatleFragment.newInstance(context,position+1);
                    addFragment(transaction,idd, fragment1, false);
                }else {
                    showFragment(transaction,idd, fragment1, false);
                }
                break;
            case 1:
                if (fragment2==null){
                    fragment2=RankThreetatleFragment.newInstance(context,position+1);
                    addFragment(transaction,idd, fragment2, false);
                }else {
                    showFragment(transaction,idd, fragment2, false);
                }
                break;
            case 2:
                if (fragment3==null){
                    fragment3=RankThreetatleFragment.newInstance(context,position+1);
                    addFragment(transaction,idd, fragment3, false);
                }else {
                    showFragment(transaction,idd, fragment3, false);
                }
                break;

            case 3:
                if (fragment4==null){
                    fragment4=RankNotatleFragment.newInstance(context,4,0);
                    addFragment(transaction,idd, fragment4, false);
                }else {
                    showFragment(transaction,idd, fragment4, false);
                }
                break;
            case 4:
                if (fragment5==null){
                    fragment5=RankThreetatleFragment.newInstance(context,position+1);
                    addFragment(transaction,idd, fragment5, false);
                }else {
                    showFragment(transaction,idd, fragment5, false);
                }
                break;
            case 5:
                if (fragment6==null){
                    fragment6=RankThreetatleFragment.newInstance(context,position+1);
                    addFragment(transaction,idd, fragment6, false);
                }else {
                    showFragment(transaction,idd, fragment6, false);
                }
                break;
            case 6:
                if (fragment7==null){
                    fragment7=RankThreetatleFragment.newInstance(context,position+1);
                    addFragment(transaction,idd, fragment7, false);
                }else {
                    showFragment(transaction,idd, fragment7, false);
                }
                break;
            case 7:
                if (fragment8==null){
                    fragment8=RankTowtatleFragment.newInstance(context,position+1);
                    addFragment(transaction,idd, fragment8, false);
                }else {
                    showFragment(transaction,idd, fragment8, false);
                }
                break;
        }



    }
    /*public void toFragment(BaseFragment f){
        getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).commit();

    }*/
    private void addFragment(FragmentTransaction transaction, int id, BaseFragment frament, boolean b) {
        if (b)
            transaction.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out, R.anim.back_left_in, R.anim.back_right_out);
        transaction.add(id, frament);
        transaction.commit();
    }
    private void hideFragments(FragmentTransaction transaction) {
        hideFragment(transaction, fragment1);
        hideFragment(transaction, fragment2);
        hideFragment(transaction, fragment3);
        hideFragment(transaction, fragment4);
        hideFragment(transaction, fragment5);
        hideFragment(transaction, fragment6);
        hideFragment(transaction, fragment7);
        hideFragment(transaction, fragment8);

    }
    private void showFragment(FragmentTransaction transaction, int id, BaseFragment frament, boolean b) {
        if (b)
            transaction.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out, R.anim.back_left_in, R.anim.back_right_out);
        transaction.show(frament);
        transaction.commit();
    }
    public void hideFragment(FragmentTransaction transaction, BaseFragment frament) {
        if (frament != null) {
            transaction.hide(frament);
        }
    }

}
